package client;

import java.util.Scanner;

public class Client {
	private static final int STATUS_OK = 0;

	public static void main(String args[]) {
		if (args.length != 1) {
			System.err.println("Params: hostname");
			System.exit(1);
		}
		Client c = new Client();
		c.start(/* Host */args[0]);
	}

	private Connection connection;

	public Client() {
		connection = new Connection();
	}

	/**
	 * Sätter upp anslutning till servern
	 */
	private void start(String hostName) {
		System.out.println("Connecting to server...");
		if (connection.connect(hostName)) {
			run();
		}
	}

	/**
	 * Visar meny, hanterar val
	 */
	private void run() {
		printHelp();
		boolean run = true;
		while (run) {
			run = handleChoice();
		}
		System.out.println("Exiting");
		connection.disconnect();
		System.out.println("Exited - Goodbye ");
	}

	/**
	 * Print menu
	 */
	private void printHelp() {
		System.out.println("Available commands: ");
		System.out.println("list        - Show available journals");
		System.out.println("get <id>    - View a journal");
		System.out.println("edit <id>   - Edit a journal");
		System.out.println("create      - Create a journal");
		System.out.println("delete <id> - Delete a journal");
		System.out.println("exit        - Exit");
	}

	private boolean handleChoice() {
		Scanner scan = new Scanner(System.in);
		System.out.print("> ");
		String cmd = scan.next().trim().toLowerCase();

		if (cmd.equals("list")) {
			listJournals();
		} else if (cmd.equals("get")) {
			showJournal(scan);
		} else if (cmd.equals("edit")) {
			editJournal(scan);
		} else if (cmd.equals("create")) {
			createJournal(scan);
		} else if (cmd.equals("delete")) {
			deleteJournal(scan);
		} else if (cmd.equals("exit")) {
			return false;
		} else {
			System.out.println("Invalid command " + cmd);
			printHelp();
		}
		return true;
	}

	private void listJournals() {
		System.out.println("Contacting server...");
		System.out.println(connection.getList().msg);
	}

	private void showJournal(Scanner scan) {
		int journalName = scan.nextInt();
		System.out.println("Getting content...");
		System.out.println(connection.viewJournal(journalName).msg);
	}

	/**
	 * Edit a journal
	 */
	private void editJournal(Scanner scan) {
		int journalName = scan.nextInt();
		System.out.println("Getting content... ");
		Response journal = connection.viewJournal(journalName);

		if (journal.status != STATUS_OK) {
			System.out.println(journal.msg);
			return;
		}
		
		EditWindow w = new EditWindow();
		String edited = w.editText(journal.msg);
		w.dispose();
		if (edited != null) {
			System.out.println("Storing new text...");
			Response response = connection.setJournalContent(journalName, edited);
			System.out.println(response.msg);
		}
	}

	private void createJournal(Scanner scan) {
		String patient = scan.nextLine().trim();
		while (patient.equals("")) {
			System.out.print("Patient name: ");
			patient = scan.nextLine().trim();
		}
		String nurse = "";
		while (nurse.equals("")) {
			System.out.print("Nurse name: ");
			nurse = scan.nextLine().trim();
		}
		Response response = connection.createJournal(patient, nurse);
		System.out.println(response.msg);
	}

	private void deleteJournal(Scanner scan) {
		int id = scan.nextInt();
		Response response = connection.deleteJournal(id);
		System.out.println(response.msg);
	}
}
